import { message } from "@/utils/message";
import { computed } from "vue";
import { submitInvoice } from "@/api/invoice";

// 需是hooks写法（函数中有return），避免失去响应性
export function useColumns(
  invoiceList: any,
  handleEditCallback?: (id: number) => void,
  handleViewCallback?: (id: number) => void,
  handleRefreshCallback?: () => void
) {
  // 发票类型映射
  const getBillTypeName = (billType: string) => {
    const billTypeMap = {
      "001": "增值税普通发票",
      "002": "增值税专用发票",
      "003": "增值税电子发票"
    };
    return billTypeMap[billType] || billType;
  };

  // 开票项目映射
  const getBillProjectName = (billProject: string) => {
    const billProjectMap = {
      "001": "会议服务费",
      "002": "搭建服务费"
    };
    return billProjectMap[billProject] || billProject;
  };

  // 使用computed来保持响应性
  const tableData = computed(() => {
    return invoiceList.value || [];
  });
  const columns: TableColumnList = [
    {
      label: "发票类型",
      prop: "bill_type",
      width: 150,
      cellRenderer: ({ row }) => {
        return getBillTypeName(row.bill_type);
      }
    },
    {
      label: "开票项目",
      prop: "bill_project",
      width: 120,
      cellRenderer: ({ row }) => {
        return getBillProjectName(row.bill_project);
      }
    },
    {
      label: "金额",
      prop: "amount",
      width: 120
    },
    {
      label: "开票信息",
      prop: "bill_info",
      width: 200
    },
    {
      label: "公司税号",
      prop: "tax_number",
      width: 180
    },
    {
      label: "注册地址",
      prop: "reg_addres",
      width: 250
    },
    {
      label: "注册电话",
      prop: "reg_phone",
      width: 140
    },
    {
      label: "开户行",
      prop: "bank",
      width: 200
    },
    {
      label: "邮寄地址",
      prop: "mail_address",
      width: 250
    },
    {
      label: "审批状态",
      prop: "status",
      width: 120,
      align: "center",
      cellRenderer: ({ row }) => {
        let statusText = "";
        let statusType = "";

        switch (row.status) {
          case 0:
            statusText = "审核中";
            statusType = "warning";
            break;
          case 1:
            statusText = "已通过";
            statusType = "success";
            break;
          case 2:
            statusText = "已退回";
            statusType = "danger";
            break;
          case 3:
            statusText = "待提交";
            statusType = "warning";
            break;
          default:
            statusText = "未知";
            statusType = "info";
        }

        return <el-tag type={statusType}>{statusText}</el-tag>;
      }
    },
    {
      label: "操作",
      fixed: "right",
      width: 220,
      cellRenderer: ({ index, row }) => (
        <>
          <el-button size="small" onClick={() => handleEdit(index + 1, row, 0)}>
            查看
          </el-button>
          <el-button size="small" onClick={() => handleEdit(index + 1, row, 1)}>
            修改
          </el-button>
          <el-button
            size="small"
            type="danger"
            onClick={() => handleSubmit(index + 1, row)}
          >
            提交审批
          </el-button>
        </>
      )
    }
  ];

  const handleEdit = (index: number, row, type: number) => {
    if (type === 0) {
      console.log(index, row);
      handleViewCallback(row.id);
    } else {
      console.log(index, row);
      // 统一拦截提示：0 审核中、1 已通过
      if (row.status === 0) {
        message("发票正在审核中，不能修改", { type: "warning" });
        return;
      }
      if (row.status === 1) {
        message("发票已通过，不能修改", { type: "warning" });
        return;
      }
      handleEditCallback(row.id);
    }
  };

  const handleSubmit = async (index: number, row) => {
    // 状态：0=审核中，1=已通过，2=已拒绝，3=待提交
    if (row.status === 0) {
      message("已提交，不可重复审批", { type: "warning" });
      return;
    }
    if (row.status === 1) {
      message("已通过，不可重复审批", { type: "warning" });
      return;
    }
    if (row.status !== 2 && row.status !== 3) {
      message("只有未通过或待提交状态才能提交审批", { type: "warning" });
      return;
    }
    try {
      const result: any = await submitInvoice(row.id, row.exhibitor_id);
      if (result.code === 0) {
        message("提交审核失败！" + result.msg, { type: "error" });
      } else {
        message("提交审核成功！", { type: "success" });
        // 这里可以触发刷新列表的回调
        if (handleRefreshCallback) {
          handleRefreshCallback();
        }
      }
    } catch (error) {
      console.error("提交审核失败:", error);
      message("提交审核失败，请重试！", { type: "error" });
    }
  };

  return {
    columns,
    tableData
  };
}
